<?php
namespace app\api\controller\csmwj;

use addons\csmwj\library\xcore\xcore\base\XcAApi;
use addons\csmwj\library\xcore\xcore\utils\XcRequestUtils;

/**
 * 工单
 *
 * @icon fa fa-circle-o
 */
class XpAdminApi extends XcAApi
{
    public function xinit(){}

    // http://127.0.0.1/fastadmin_plugin_1.2.0.20201008/api/csmwj/xp_admin_api/paperquestionjson?id=1
    /**
     * form-generate启动时根据paperid按照其格式要求读取fields json
     * http://127.0.0.1/fastadmin_plugin_1.2.0.20201008/api/csmwj/xp_admin_api/paperquestionjson?id=1
     */
    public function getpaperquestionjson()
    {
        $id = XcRequestUtils::get('id', true);

        $questionlist = null;
        if (true) {
            $dao = new \app\admin\model\csmwj\Question();
            $questionlist = $dao->where('csmwj_paper_id', '=', $id)
                ->where('status', '=', 'normal')
                ->order('weigh', 'desc')
                ->select();
        }

        $fields = [];
        foreach ($questionlist as $item) {
            $fields[] = empty($item->field) ? [] : json_decode($item->field, true);
        }

        $this->success(null,[
            'fields' => $fields
        ]);
        // return '';
    }

    /**
     * form-generate保存工单提交
     *
     * spa. 根据fieldcode来判定update or create ,并修改排序
     * spb. 删除没有更新的记录
     */
    public function savepaperquestionjson()
    {
        $id = XcRequestUtils::get('id', true);
        $body = XcRequestUtils::requestBody();
        $fieldsjson = json_decode($body, true);
        //$fieldsjson = json_decode($this->cRequestBody(), true);

        $dao = new \app\admin\model\csmwj\Question();

        // spa
        $fieldcodes = [];
        if (true) {
            $count = count($fieldsjson);

            foreach ($fieldsjson as $index => $item) {
                // var_dump($item['__config__']);
                $fieldcodes[] = $item['__vModel__'];
                $row = $dao->where('csmwj_paper_id', '=', $id)
                    ->where('status', '=', 'normal')
                    ->where('fieldcode', '=', $item['__vModel__'])
                    ->find();
                $pp = [
                    'csmwj_paper_id' => $id,
                    'title' => isset($item['csmwjtitle']['value']) ? $item['csmwjtitle']['value'] : null,
                    'fieldcode' => $item['__vModel__'],
                    'csmtype' => $item['__config__']['csmtype'],
                    'fieldtag' => $item['__config__']['tag'],
                    'weigh' => $count - $index,
                    'field' => json_encode($item)
                ];
                if ($row) {
                    $dao->where('id', '=', $row->id)->update($pp);
                } else {
                    // var_dump($pp);
                    $dao->create($pp);
                }
            }
        }

        // spb.
        if (true) {
            $list = $dao->where('csmwj_paper_id', '=', $id)
                ->where('status', '=', 'normal')
                ->where('fieldcode', 'not in', $fieldcodes)
                ->select();

            foreach ($list as $item) {
                // var_dump($dao->where('id', '=', $item->id)->find());
                $dao->where('id', '=', $item->id)->update([
                    'status' => 'hidden'
                ]);
            }
        }

        $this->success(null);
    }
}
